using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayerSounds : MonoBehaviour
{
	private SfxManager sfx;

	public AudioSource audioStep;

	public AudioSource audioFastSound;

	public AudioSource audioLoopSound;

	private ThirdPersonController tController;

	private AudioClip curAudioDamage;

	private float lastTime = Time.timeSinceLevelLoad;

	private float curDist = 10f;

	private bool needPlaySoundStep = true;

	private bool needPlaySoundFast = true;

	private bool needPlaySoundLoop = true;

	private void Start()
	{
		sfx = GetComponentInParent<SfxManager>();
		tController = GetComponentInParent<ThirdPersonController>();
		UpdateSoundDamage();
		if (!SceneManager.GetActiveScene().name.Equals("Lobby"))
		{
			StartCoroutine(CheckPlaySound());
		}
	}

	public void UpdateSoundDamage()
	{
		if (sfx != null)
		{
			curAudioDamage = sfx.GetSoundSfx(SfxId.damage);
		}
	}

	private IEnumerator CheckPlaySound()
	{
		while (true)
		{
			if (GameController.instance != null && GameController.instance.myPlayer != null)
			{
				curDist = Vector3.Distance(base.transform.position, GameController.instance.myPlayer.transform.position);
				needPlaySoundStep = curDist < audioStep.maxDistance;
				needPlaySoundFast = curDist < audioFastSound.maxDistance;
				needPlaySoundLoop = curDist < audioLoopSound.maxDistance;
			}
			yield return new WaitForSeconds(0.3f);
		}
	}

	public void Step()
	{
		StepPlayer();
	}

	public void StepPlayer()
	{
		if (Device.indexWeakDevice == 0 && needPlaySoundStep && audioStep != null && sfx != null && Time.timeSinceLevelLoad - lastTime > 0.2f)
		{
			audioStep.enabled = true;
			if (tController != null)
			{
				audioStep.volume = tController.moveSpeed / tController.WalkSpeed;
			}
			if (!RoomTrigger.playerInRoom && (SceneManager.GetActiveScene().name.Equals("Level1") || SceneManager.GetActiveScene().name.Contains("Level1_Offline")))
			{
				audioStep.PlayOneShot(sfx.GetSoundSfx(SfxId.walk));
			}
			else
			{
				audioStep.PlayOneShot(sfx.GetSoundSfx(SfxId.walkNormal));
			}
			lastTime = Time.timeSinceLevelLoad;
		}
	}

	public void Damage()
	{
		if (needPlaySoundFast && audioFastSound != null && sfx != null)
		{
			if (curAudioDamage == null)
			{
				UpdateSoundDamage();
			}
			if (curAudioDamage != null)
			{
				audioFastSound.PlayOneShot(curAudioDamage);
			}
		}
	}

	public void ResetLoopSound()
	{
		if (needPlaySoundLoop && audioLoopSound != null && audioLoopSound.clip != null)
		{
			audioLoopSound.clip = null;
			audioLoopSound.Stop();
		}
	}

	public void Falling()
	{
		if (needPlaySoundFast && audioFastSound != null && sfx != null)
		{
			audioFastSound.PlayOneShot(sfx.GetSoundSfx(SfxId.falling));
		}
	}

	public void Death()
	{
		if (needPlaySoundFast && audioFastSound != null && sfx != null)
		{
			audioFastSound.PlayOneShot(sfx.GetSoundSfx(SfxId.death));
		}
	}

	public void Ladder()
	{
		if (needPlaySoundFast && audioFastSound != null && sfx != null)
		{
			audioFastSound.PlayOneShot(sfx.GetSoundSfx(SfxId.ladder));
		}
	}

	public void Roll()
	{
		if (needPlaySoundFast && audioFastSound != null && sfx != null)
		{
			if (tController != null && tController.pBehavior != null && tController.pBehavior.inWater)
			{
				audioFastSound.PlayOneShot(sfx.GetSoundSfx(SfxId.swimRoll));
			}
			else
			{
				audioFastSound.PlayOneShot(sfx.GetSoundSfx(SfxId.roll));
			}
		}
	}

	public void Idle()
	{
		if (needPlaySoundFast && audioLoopSound != null && sfx != null)
		{
			audioLoopSound.clip = sfx.GetSoundSfx(SfxId.idle);
			audioLoopSound.Play();
		}
	}

	public void Jump()
	{
		if (needPlaySoundFast && audioFastSound != null && sfx != null)
		{
			audioFastSound.PlayOneShot(sfx.GetSoundSfx(SfxId.jump));
		}
	}

	public void SwimMove()
	{
		if (needPlaySoundStep && audioStep != null && sfx != null && Time.timeSinceLevelLoad - lastTime > 0.2f)
		{
			audioStep.enabled = true;
			if (tController != null)
			{
				audioStep.volume = tController.moveSpeed / tController.WalkSpeed;
			}
			audioStep.PlayOneShot(sfx.GetSoundSfx(SfxId.swimMove));
			lastTime = Time.timeSinceLevelLoad;
		}
	}

	public void SwimIdle()
	{
		if (needPlaySoundFast && audioFastSound != null && sfx != null)
		{
			audioFastSound.PlayOneShot(sfx.GetSoundSfx(SfxId.swimIdle));
		}
	}
}
